from PyQt5.QtCore import pyqtSlot
import logging
#include "DeviceAddModel.h"
#include "DeviceAddItem.h"
#include <QCoreApplication>
#include <QEventLoop>
#include <QFile>
#include <QHostAddress>
#include <QString>
#include <chrono>
#include <random>
from QuickListModel import QuickListModel
# const static QString	 nameTemplate("item %1");
# const static QString	 ipTemplate("%1.%2.%3.%4");
# const static QString	 modelTemplate("model %1");
# from myglobal import MySet
# class DeviceAddModelPrivate
# {
# public:
# 	std.default_random_engine				randomEngine;
# 	std.uniform_int_distribution<uint32_t> u65535 { 0, 0xffffffff };
# };
# public slots:
# 	void doUpdateName(int row, const QString& name);

# 	void initData();

# 	void addOne();
# 	void addMulti(int count);

# 	void insertBeforeSelected();
# 	void insertBeforeRow(int row);

# 	void clearAll();

# 	void removeSelected();
# 	void removeChecked();
# 	void removeRow(int row);
from QuickListItemBase import QuickListItemBase
class Item(QuickListItemBase):
	def __init__(self,name,address,modelString,parent=None):
		super().__init__(parent)
		self.name=name
		self.address=address
		self.modelString=modelString
class DeviceAddModel(QuickListModel):
	def __init__(self,parent=None):
		super().__init__(parent)
		self._headerRoles=["name", "address", "modelString" ]
		self._sortRole="name"
		maps={}
		maps["name"] =lambda b1,b2: b1.name()<b2.name()
		maps["address"] =lambda d1,d2:d1.toIPv4Address() < d2.toIPv4Address()
		maps["modelString"] = lambda d1,d2:d1.modelString() < d2.modelString()
		super().setSortCallbacksAscend(maps)
	@pyqtSlot()
	def initData(self):
		N = 5#00;
		objs=[]
		# objs.reserve(N);
		# c1 = std.chrono.high_resolution_clock.now();
		for i in range(N):#(int i = 0; i < N; ++i)
			item = self.genOne(i);
			objs.append(item)
		# c2	   = std.chrono.high_resolution_clock.now();
		# micro = std.chrono.duration_cast<std.chrono.milliseconds>(c2 - c1).count();
		# qWarning() << "general" << N << "cost" << micro << "ms";
		self.resetData(objs);
		self.sortByRole();
	@pyqtSlot()
	def addOne(self):
		item = self.genOne(1);
		self.appendMany([item]);
	@pyqtSlot(int)
	def addMulti(self,count):
		if count<1:
			return
		objs=[]# QList<QuickListItemBase*> objs;
		# objs.reserve(count);

		for i in range(count):#(int i = 0; i < count; ++i)
			item = self.genOne(i);
			objs.append(item)
		logging.info(objs)
		self.appendMany(objs)
	@pyqtSlot()
	def insertBeforeSelected(self):
		if len(self.mDatas) == 0:
			item = self.genOne(1);
			self.insert_many(0, [item])
		else:
			pos = -1;
			for i in range(len(self.mDatas)):
				obj = self.mDatas[i]
				if obj["isSelected"]:
					pos = i;
					break;
			if (pos >= 0):
				item = self.genOne(1);
				self.insert_many(pos, [item])
	@pyqtSlot(int)
	def insertBeforeRow(self, row):
		item = self.genOne(1);
		self.insert_many(row, [item])
	@pyqtSlot()
	def clearAll(self):
		self.clear();
	@pyqtSlot()
	def removeSelected(self):
		toRemove=[]
		for i in range(len(self.mDatas)):
			obj = self.mDatas[i]
			if obj["isSelected"]:
				toRemove.append(i)
			else:
				pass
				# ++i;
		toRemove.reverse()#from big to small
		for one in toRemove:
			self.removeAt(one);

	@pyqtSlot()
	def removeChecked(self):
		for i in range(len(self.mDatas)):
			obj = self.mDatas[i]
			if obj["isChecked"]:
				self.removeAt(i);
			else:
				pass
				# ++i;
	@pyqtSlot(int)
	def removeRow(self, row):
		self.removeAt(row);

	def genOne(self, value):
		# item=Item()
		# item={"online":0}
		# item = DeviceAddItem()
		# item.set_online(value % 2 == 0);
		# item.set_name(nameTemplate.arg(value));
		# item["isSelected"]=False
		# item["isChecked"]=False
		# item["isAlternate"]=False
		name="name%d" % value
		ip4 = value % 256;
		ip3 = value / 256 % 256;
		ip2 = value / 256 / 256 % 256;
		ip1 = value / 256 / 256 / 256 % 256;
		address="%d-%d-%d-%d" % (ip1,ip2,ip3,ip4)
		if (value % 2 == 0): 
			modelString=value 
		else:
			modelString=0xffffffff - value
		item={}
		item["isSelected"]=False
		item["isChecked"]=False
		item["isAlternate"]=False
		item["name"]=name
		item["address"]=address
		item["modelString"]=modelString
		return item
		# return Item(name,address,modelString)
	@pyqtSlot(int,str)
	def doUpdateName(self, row, name):
		if (row < 0 or row >= rowCount({})):
			return;
		n = name.simplified();
		if (n.isEmpty()):
			return;
		mDatas.at(row).set_name(name)
